<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            padding: 10px;
        }

        .image-container {
            text-align: center;
            margin-top: 20px;
        }

        img {
            max-width: 100%;
            height: auto;
            border: 2px solid #ddd;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
        }

        .file-input-container {
            text-align: center;
            margin-top: 15px;
        }

        input[type="file"] {
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 5px;
            background-color: #fff;
            width: 100%;
        }

        .upload-button {
            display: block;
            width: 100%;
            padding: 10px;
            margin-top: 15px;
            border: none;
            border-radius: 5px;
            background-color: #007bff;
            color: #fff;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .upload-button:hover {
            background-color: #0056b3;
        }
    </style>
</head>

<body>
<div class="image-container">
    <img id="previewImage" alt="Selected Image" width="100%" height="auto">
</div>
<div class="file-input-container">
    <input type="file" id="fileInput">
</div>
<button class="upload-button" onclick="uploadImage()">上传</button>
<button class="upload-button" onclick="getMain()">查看监测结果</button>

<script>
    const fileInput = document.getElementById('fileInput');
    const previewImage = document.getElementById('previewImage');

    fileInput.addEventListener('change', function (event) {
        const file = event.target.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function (e) {
                previewImage.src = e.target.result;
            };
            reader.readAsDataURL(file);
        }
    });

    function uploadImage() {
        const file = fileInput.files[0];
        if (!file) {
            alert('请先选择图片！');
            return;
        }
        const formData = new FormData();
        formData.append('img', file);
//        fetch('http://localhost:8050/alarm/message/callback', {
        fetch('http://3yumjv.natappfree.cc/alarm/message/callback', {
            method: 'POST',
            body: formData,
        })
        .then(response => {
            console.log('Response status:', response.status);
            console.log('Response headers:', response.headers);
            if (response.ok) {
                return response.json();
            } else {
                throw new Error('上传失败');
            }
        })
        .then(data => {
            console.log('Received data:', data);
            alert('上传成功！');
        })
        .catch(error => {
            console.error('Error details:', error);
            alert('上传失败!'+error);
        });
    }

    function getMain(){
        window.location.href = "http://3yumjv.natappfree.cc/main";
    }
</script>
</body>

</html>